package com.wuster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuster.entity.Admin;
import com.wuster.service.AdminService;
import com.wuster.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author Admin
* @description 针对表【admin】的数据库操作Service实现
* @createDate 2024-07-04 21:14:08
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Autowired
    private AdminMapper adminMapper;


    //根据管理员名查找管理员信息
    public Admin selectAdminByName(String AdminName){
        QueryWrapper<Admin> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",AdminName);
        Admin admin = adminMapper.selectOne(queryWrapper);
        return admin;
    }



    //修改管理员密码
    public void updateAdminPassword(String AdminName,String newPassword){
        //然后更新密码,更新管理员的密码和更新时间
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        //可能时间类型有冲突
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        updateWrapper.eq("name",AdminName)
                .set("password",newPassword)
                .set("update_time", Timestamp.valueOf(dateFormat.format(date)));
        int result = adminMapper.update(null, updateWrapper);
    }

}




